package design.pattern.Lu09CommandPattern;

/**
 * @program JavaBase
 * @description: 命令模式 todo|undo
 * @author: zhanglu
 * @create: 2020-03-30 09:45:00
 */
public class Lu09CommandPattern02 {

    public static void main(String[] args) {
        Receiver02 receiver02 = new ReceiverImpl02();

        Command02 command02 = new AddCommand02(receiver02);

        command02.todo();
        command02.undo();


    }

}

interface Receiver02{

    void add();

    void sub();

}

interface Command02{

    void todo();

    void undo();

}

class ReceiverImpl02 implements Receiver02{

    @Override
    public void add() {
        System.out.println("add");
    }

    @Override
    public void sub() {
        System.out.println("sub");
    }
}

class AddCommand02 implements Command02{

    private Receiver02 receiver02;

    public AddCommand02(Receiver02 receiver02){
        this.receiver02 = receiver02;
    }

    @Override
    public void todo() {
        this.receiver02.add();
    }

    @Override
    public void undo() {
        this.receiver02.sub();
    }
}

class SubCommand02 implements Command02{

    private Receiver02 receiver02;

    public SubCommand02(Receiver02 receiver02){
        this.receiver02 = receiver02;
    }

    @Override
    public void todo() {
        this.receiver02.sub();
    }

    @Override
    public void undo() {
        this.receiver02.add();
    }
}

